package com.sheep.util;

import com.sheep.api.bean.UserBean;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.log4j.Logger;

import java.io.File;

/**
 * Created by Administrator on 2016/3/25.
 */
public class ConfSupport {
    private static final Logger logger = Logger.getLogger(ConfSupport.class);
    private static String defaultAddress = null;
    private static String trackerAddress = null;

    private static CompositeConfiguration config = new CompositeConfiguration();

    static {
        try {
            String path = "conf/application.properties";
            File f = new File(path);
            if (f.exists()) {
                config.addConfiguration(new PropertiesConfiguration(path));
            }
        } catch (Exception e) {
            logger.error("配置解析失败" + e.getMessage());
        }
    }

    public static CompositeConfiguration getConfig() {
        return config;
    }

    public static Configuration getConf() {
        Configuration conf = new Configuration();
        conf.set("fs.default.name", getDefaultAddress());
        conf.set("mapred.job.tracker", getTrackerAddress());
        conf.set(Constant.PRE_PATH, TimeSupport.getDateStringForPath() + "/");
        return conf;
    }

    public static String getUserHDFSAddress(UserBean user) {
        return getDefaultAddress() + "/user/" + user.getName();
    }


    public static String getDefaultAddress() {
        if (defaultAddress == null) {
            defaultAddress = getConfig().getString("defaultAddress", "hdfs://localhost:9000");
        }
        return defaultAddress;
    }

    public static String getTrackerAddress() {
        if (trackerAddress == null) {
            trackerAddress = getConfig().getString("trackerAddress", "hdfs://localhost:9001");
        }
        return trackerAddress;
    }
}
